package web.ctroller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.UUID;

@Controller
@RequestMapping("/file")
public class FileController {


    @RequestMapping("/upload1")
    @ResponseBody
    public String upload1(MultipartFile file1,String name) throws IOException {
//        System.out.println(file1);
//        System.out.println(name);

        //获取上传的文件名称
        String originalFilename = file1.getOriginalFilename(); //xx.jpg
        //上传文件时表单项的名字(没用)
        String name1 = file1.getName();
        //上传文件的大小
        long size = file1.getSize();
        //上传文件的数据流
//        InputStream inputStream = file1.getInputStream();

        System.out.println("originalFilename:" + originalFilename);
        System.out.println("name1:" + name1);
        System.out.println("size:" + size);

        //保存文件
        //保证文件名唯一性（避免文件覆盖问题） -- uuid
        String uuid = UUID.randomUUID().toString().replace("-","");
        int lastDotIdx = originalFilename.lastIndexOf(".");
        String fileSuffix = originalFilename.substring(lastDotIdx);

        String fileName = uuid+fileSuffix;

        File saveFile = new File("C:\\Users\\aming\\Desktop\\" + fileName);
        file1.transferTo(saveFile);

        return "success";
    }
}
